package com.fgs.common;

import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.fgs.common.utils.CommonUtil;
import com.fgs.common.widget.BackToTopView;

import org.yczbj.ycrefreshviewlib.adapter.RecyclerArrayAdapter;
import org.yczbj.ycrefreshviewlib.inter.OnLoadMoreListener;
import org.yczbj.ycrefreshviewlib.item.RecycleViewItemLine;
import org.yczbj.ycrefreshviewlib.view.YCRefreshView;

import java.util.List;

/**
 * @author fgs
 * @date 2020/3/29
 * @description
 **/
public abstract class CommonDataListFragment<V extends CommonView, P extends CommonPresenter<V>> extends CommonFragment<V, P> implements CommonListView {
    protected BackToTopView mBackToTopView;
    protected YCRefreshView mRecyclerView;
    protected TextView tv_money_daishou;
    protected RecyclerArrayAdapter mAdapter;
    private int mPage = 1;
    private int mTotalPage;
    private boolean isRefresh;

    @Override
    public final void init() {

        mBackToTopView = getView().findViewById(R.id.layout_data_list_backToTopView);
        mRecyclerView = getView().findViewById(R.id.layout_data_list_recyclerView);

        tv_money_daishou = getView().findViewById(R.id.tv_money_daishou);

        initData();
        mAdapter = createAdapter();
        if (mAdapter == null) return;
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.setAdapter(mAdapter);
        RecycleViewItemLine line = new RecycleViewItemLine(getContext(),
                LinearLayout.HORIZONTAL,
                (int) CommonUtil.convertDpToPixel(getItemLineHeight()),
                getResources().getColor(getItemLineColor())
        );
        if (isNeedRecycleViewItemLine()) {
            mRecyclerView.addItemDecoration(line);
        }

        mRecyclerView.setRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshOperate();
                mPage = 1;
                isRefresh = true;
                requestDataList(mPage, false);
            }
        });
        mAdapter.setMore(R.layout.layout_load_more, new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                if (mPage < mTotalPage) {
                    mPage++;
                    requestDataList(mPage, false);
                } else {
                    mAdapter.stopMore();
                }
            }
        });
        mAdapter.setNoMore(R.layout.layout_no_more);


        mBackToTopView.setRecyclerView(mRecyclerView.getRecyclerView(), 0);
        initListener();

        requestDataList(1, true);
    }

    //是否添加列表分割线
    public boolean isNeedRecycleViewItemLine() {
        return true;
    }

    //列表分割线颜色
    public int getItemLineColor() {
        return R.color.colorGrayOne;
    }

    protected int getItemLineHeight() {
        return 10;
    }


    @Override
    public void showDataList(List data) {
        if (mAdapter == null) return;
        if (isRefresh) {
            mAdapter.clear();
            isRefresh = false;
        }
        mAdapter.addAll(data);
    }

    @Override
    public void setPageInfo(int currentPage, int totalPage) {
        mPage = currentPage;
        mTotalPage = totalPage;
    }

    /**
     * 刷新列表数据时调用
     */
    public void refreshData(boolean isDialog) {
        isRefresh = true;
        requestDataList(1, isDialog);
    }
    public void setRefresh(boolean isRefresh){
        this.isRefresh = isRefresh;
    }
    public boolean getIsRefresh(){
        return isRefresh;
    }
    public void showEmptyView() {

        if (null != mAdapter) {
            mAdapter.clear();
        }

        if (isRefresh) {
            isRefresh = false;
        }


        if (mRecyclerView == null) return;
        mRecyclerView.setEmptyView(R.layout.layout_empty_view);
        mRecyclerView.showEmpty();

        View emptyView = mRecyclerView.getEmptyView();
        if (emptyView != null) {
            int imageId = getEmptyViewImageId();
            if (imageId != 0) {
                emptyView.findViewById(R.id.layout_empty_view_imageView).setBackgroundResource(getEmptyViewImageId());
            }
            TextView textView = mRecyclerView.getEmptyView().findViewById(R.id.layout_empty_view_textView);
            String content = getEmptyViewContent();
            if (!TextUtils.isEmpty(content)) {
                textView.setText(content);
            }
        }
    }

    public abstract RecyclerArrayAdapter createAdapter();

    public RecyclerArrayAdapter getAdapter() {
        return mAdapter;
    }

    protected abstract void initListener();

    public abstract void requestDataList(int page, boolean isDialog);

    public abstract void initData();

    //刷新后可能进行的操作
    public void refreshOperate() {
    }

    @Override
    public void stopRefresh() {
        if (mRecyclerView != null) {
            mRecyclerView.setRefreshing(false);
        }
//        mRecyclerView.showRecycler();
    }

    protected int getEmptyViewImageId() {
        return R.mipmap.icon_empty_route;
    }

    protected String getEmptyViewContent() {
        return "";
    }

    public boolean isRefresh() {
        return isRefresh;
    }
}
